package org.jgs1904.apachepoidemo.poi;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Calendar;
import java.util.Date;

/**
 * 填充和颜色
 *
 * @author: 默苍璃
 * @date: 2023-10-09 15:29
 */
public class FillAndColor {

    public static void main(String[] args) throws IOException {
        Workbook wb = new XSSFWorkbook();
        Sheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
        Row row = sheet.createRow(1);
// Aqua background
        CellStyle style = wb.createCellStyle();
        style.setFillBackgroundColor(IndexedColors.AQUA.getIndex());
        style.setFillPattern(FillPatternType.BIG_SPOTS);
        Cell cell = row.createCell(1);
        cell.setCellValue("X");
        cell.setCellStyle(style);
// Orange "foreground", foreground being the fill foreground not the font color.
        style = wb.createCellStyle();
        style.setFillForegroundColor(IndexedColors.ORANGE.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        cell = row.createCell(2);
        cell.setCellValue("X");
        cell.setCellStyle(style);
// Write the output to a file
        try (OutputStream fileOut = new FileOutputStream("D:\\temp\\workbookFillAndColor.xls")) {
            wb.write(fileOut);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        wb.close();

    }


    /**
     * 合并单元格
     */
    @Test
    public void test01() throws IOException {
        Workbook wb = new HSSFWorkbook();
        Sheet sheet = wb.createSheet("new sheet");
//        Row row = sheet.createRow(1);
//        Cell cell = row.createCell(1);
//        cell.setCellValue("This is a test of merging");
        sheet.addMergedRegion(new CellRangeAddress(1, //first row (0-based)
                3, //last row  (0-based)
                3, //first column (0-based)
                5  //last column  (0-based)
        ));

        sheet.addMergedRegion(new CellRangeAddress(0, //first row (0-based)
                1, //last row  (0-based)
                0, //first column (0-based)
                2  //last column  (0-based)
        ));

        // 获取第一行并创建单元格填写内容
        Row row0 = sheet.getRow(1);
        if (row0 == null) {
            row0 = sheet.createRow(1);
        }
        Cell cell0 = row0.createCell(3);
        cell0.setCellValue("This is a test of merging");

// 继续编写补充的代码
// 例如，可以创建新的行和单元格，并填写所需的内容
        Row row1 = sheet.createRow(0);
        Cell cell1 = row1.createCell(0);
        cell1.setCellValue("cell1");

        // 例如，可以创建新的行和单元格，并填写所需的内容
        Row row2 = sheet.getRow(0);
        Cell cell2 = row2.createCell(3);
        cell2.setCellValue("cell2");

// Write the output to a file
        try (OutputStream fileOut = new FileOutputStream("D:\\temp\\CellRangeAddress.xls")) {
            wb.write(fileOut);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        wb.close();

    }


    /**
     * 设置字体
     */
    @Test
    public void test02() throws IOException {
        Workbook wb = new HSSFWorkbook();
        Sheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
        Row row = sheet.createRow(1);
// Create a new font and alter it.
        Font font = wb.createFont();
        font.setFontHeightInPoints((short) 24);
        font.setFontName("Courier New");
        font.setItalic(true);
        font.setStrikeout(true);
// Fonts are set into a style so create a new one to use.
        CellStyle style = wb.createCellStyle();
        style.setFont(font);
// Create a cell and put a value in it.
        Cell cell = row.createCell(1);
        cell.setCellValue("This is a test of fonts");
        cell.setCellStyle(style);
// Write the output to a file
        try (OutputStream fileOut = new FileOutputStream("D:\\temp\\workbookFont.xls")) {
            wb.write(fileOut);
        }
        wb.close();

    }


    /**
     * 自定义颜色
     */
    @Test
    public void test03() throws IOException {
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet();
        HSSFRow row = sheet.createRow(0);
        HSSFCell cell = row.createCell(0);
        cell.setCellValue("Default Palette");
//apply some colors from the standard palette,
// as in the previous examples.
//we'll use red text on a lime background
        HSSFCellStyle style = wb.createCellStyle();
//        style.setFillForegroundColor(HSSFColor.LIME.index);
        style.setFillForegroundColor(HSSFColor.HSSFColorPredefined.LIME.getIndex());
        style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        HSSFFont font = wb.createFont();
        font.setColor(HSSFColor.HSSFColorPredefined.RED.getIndex());
        style.setFont(font);
        cell.setCellStyle(style);
//save with the default palette
        try (OutputStream out = new FileOutputStream("D:\\temp\\default_palette.xls")) {
            wb.write(out);
        }
//now, let's replace RED and LIME in the palette
// with a more attractive combination
// (lovingly borrowed from freebsd.org)
        cell.setCellValue("Modified Palette");
//creating a custom palette for the workbook
        HSSFPalette palette = wb.getCustomPalette();
//replacing the standard red with freebsd.org red
        palette.setColorAtIndex(HSSFColor.HSSFColorPredefined.RED.getIndex(), (byte) 153,  //RGB red (0-255)
                (byte) 0,    //RGB green
                (byte) 0     //RGB blue
        );
//replacing lime with freebsd.org gold
        palette.setColorAtIndex(HSSFColor.HSSFColorPredefined.LIME.getIndex(), (byte) 255, (byte) 204, (byte) 102);
//save with the modified palette
// note that wherever we have previously used RED or LIME, the
// new colors magically appear
        try (OutputStream out = new FileOutputStream("D:\\temp\\modified_palette.xls")) {
            wb.write(out);
        }

    }


}
